﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Common;
using BusinessLayer;
using BoletoElectronicoDesktop.AbmPost_Net;

namespace BoletoElectronicoDesktop.AbmBeneficiarios
{
    public partial class FormBeneficiariosList : FormListDelete
    {
        private const int habilitado = 1;
        private const int desHabilitado = 0;

        public FormBeneficiariosList(Util.Funcionalidad use) :base(use)
        {
            incializarFormulario();
        }

        private void incializarFormulario()
        {
            InitializeComponent();
            cargarCbRubro();
        }

        private void cargarCbRubro()
        {
            cbRubros.DataSource = BizRubro.getRubros();
            cbRubros.SelectedItem = null;
        }

        private void btnNewBenef_Click(object sender, EventArgs e)
        {
            try
            {
                FormAbmBeneficarios frm;
                if (this.MdiParent.MdiChildren.Any(f => f is FormAbmBeneficarios))
                {
                    frm = (FormAbmBeneficarios)this.MdiParent.MdiChildren.First(f => f is FormAbmBeneficarios);
                }
                else
                {
                    frm = new FormAbmBeneficarios(null, Util.Funcionalidad.BeneficiarioAlta);
                    frm.MdiParent = this.MdiParent;
                }

                frm.Show();
            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnBuscarPN_Click(object sender, EventArgs e)
        {
            try
            {
                FormPostnetList frm;
                if (this.MdiParent.MdiChildren.Any(f => f is FormPostnetList))
                {
                    frm = (FormPostnetList)this.MdiParent.MdiChildren.First(f => f is FormPostnetList);
                }
                else
                {
                    frm = new FormPostnetList(Util.Funcionalidad.PostNetSelectOne);
                    frm.btnBuscar_Click(new Object(), new EventArgs());
                    frm.MdiParent = this.MdiParent;
                }

                frm.Show();
            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        internal void setPostNetSelected(dsBoleto.PostnetRow pr)
        {
            txtPostNet.Text = pr.IdPostnet.ToString();
            txtMarca.Text = pr.Marca;
            txtModelo.Text = pr.Modelo;
        }

        public void btnBuscar_Click(object sender, EventArgs e)
        {
            String razonSocial = txtRazonSocial.Text.ToString();
            
            int idRubro = 0;
            if (cbRubros.SelectedItem != null)
                idRubro = Convert.ToInt32(cbRubros.SelectedValue.ToString());

            int idPostnet = 0;
            if (!String.IsNullOrEmpty(txtPostNet.Text))
                idPostnet = Convert.ToInt32(txtPostNet.Text.ToString());

            String calle = txtCalle.Text.ToString();

            int numero = 0;
            if(!String.IsNullOrEmpty(txtNumero.Text))
                Convert.ToInt32(txtNumero.Text);

            int piso = 0;
            if(!String.IsNullOrEmpty(txtPiso.Text))
                piso = Convert.ToInt32(txtPiso.Text);

            String depto = txtDepto.Text;

            dsBoleto.BeneficiarioDataTable dtBenef =
                BizBeneficiario.getBeneficiarios(razonSocial, idRubro, idPostnet, calle, numero, piso, depto, habilitado);

            dgViewBeneficiarios.DataSource = dtBenef;
        }

        private void dgViewBeneficiarios_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1)
                OnDGWSelectedItem();
        }

        private void OnDGWSelectedItem()
        {
            dsBoleto.BeneficiarioRow br = (dsBoleto.BeneficiarioRow)((DataRowView)(dgViewBeneficiarios.CurrentRow.DataBoundItem)).Row;
            switch (dgViewBeneficiarios.CurrentCell.ColumnIndex)
            {
                case 5://Edit
                    btnEdit_Click(br);
                    break;
                case 6://Delete
                    if (UserAllowed(Util.Funcionalidad.BeneficiarioBaja))
                    {
                        btnEliminar_Click(br.IdBeneficiario);
                    }
                    else
                    {
                        MessageBox.Show("Ud. No tiene permisos como para realizar esta operacion.-");
                    }
                    break;

                default:
                    break;
            }
        }

        private void btnEdit_Click(dsBoleto.BeneficiarioRow br)
        {
            try
            {
                FormAbmBeneficarios frm;
                if (this.MdiParent.MdiChildren.Any(f => f is FormAbmBeneficarios))
                {
                    frm = (FormAbmBeneficarios)this.MdiParent.MdiChildren.First(f => f is FormAbmBeneficarios);
                }
                else
                {
                    frm = new FormAbmBeneficarios(br, Util.Funcionalidad.BeneficiarioModificacion);
                    frm.MdiParent = this.MdiParent;
                }

                frm.Show();
            }
            catch (ApplicationException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnEliminar_Click(int idBeneficiario)
        {
            DialogResult result = MessageBox.Show("¿Esta Seguro que desea eliminar al beneficiario seleccionado?",
                                             "Eliminar Beneficiario", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.Yes)
            {
                bool deleted = BizBeneficiario.removeBeneficiario(idBeneficiario, desHabilitado);
                if (deleted)
                {
                    int ind = dgViewBeneficiarios.CurrentRow.Index;
                    dgViewBeneficiarios.Rows.RemoveAt(ind);
                }
                else
                {
                    MessageBox.Show("No se pudo eliminar");
                }
            }
        }

        private void btnLimpiar_Click(object sender, EventArgs e)
        {
            txtRazonSocial.Clear();
            txtPostNet.Clear();
            txtMarca.Clear();
            txtModelo.Clear();
            txtNumero.Clear();
            txtPiso.Clear();
            txtCalle.Clear();
            txtDepto.Clear();
        }

        private void txtNumerico_KeyPress(object sender, KeyPressEventArgs e)
        {
            Util.limitarCampoNumericoEntero(e);
        }

        private void txtTexto_KeyPress(object sender, KeyPressEventArgs e)
        {
            Util.limitarCampoTexto(e);
        }
    }
}
